﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

using KxzzEntity.SettingEntity;
using Kaixin001.XmlUtil;
using Kaixin001.Security;
using DevComponents.DotNetBar.Rendering;
using Kxzz.Common;

namespace Kxzz.SettingForm
{
    public partial class frmFetionSetting : DevComponents.DotNetBar.Office2007Form
    {
        #region 变量
        private IniHelper iniHelper = new IniHelper(Application.StartupPath + "//Config//config.ini");
        private List<FetionSettingEntity> fetionSettings = new List<FetionSettingEntity>();
        #endregion

        #region 构造函数
        public frmFetionSetting()
        {
            InitializeComponent();

            if (this.iniHelper.read("Setup", "skin") != "")
            {
                eOffice2007ColorScheme colorScheme = (eOffice2007ColorScheme)Enum.Parse(typeof(eOffice2007ColorScheme), this.iniHelper.read("Setup", "skin"));
                this.Office2007ColorTable = colorScheme;
            }
        }

        private void frmFetionSetting_Load(object sender, EventArgs e)
        {
            this.FormInit();
        }
        #endregion

        #region 界面操作
        private void txtBeforeMinutes_KeyPress(object sender, KeyPressEventArgs e)
        {
            //限制只能输入数字 
            if (char.IsNumber(e.KeyChar) == false)
            {
                if (e.KeyChar != (char)46 && e.KeyChar != (char)8)
                {
                    e.Handled = true;
                }
            }
        }

        private void txtFetionId_KeyPress(object sender, KeyPressEventArgs e)
        {
            //限制只能输入数字 
            if (char.IsNumber(e.KeyChar) == false)
            {
                if (e.KeyChar != (char)46 && e.KeyChar != (char)8)
                {
                    e.Handled = true;
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.txtBeforeMinutes.Text == "" ||
                this.txtFetionId.Text == "" ||
                this.txtFetionPass.Text == "")
            {
                DevComponents.DotNetBar.MessageBoxEx.Show(
                       "请将所有信息填写完整！",
                       "提示",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
                this.txtFetionId.Focus();
                return;
            }

            bool result = this.SaveFetionSetting(this.GetFetionSetting());

            if (result)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show(
                        "保存代理信息成功！",
                        "提示",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information);
            }
            else
            {
                DevComponents.DotNetBar.MessageBoxEx.Show(
                       "保存代理信息失败！",
                       "提示",
                       MessageBoxButtons.OK,
                       MessageBoxIcon.Information);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Form main = (Form)this.ParentForm;
            main.Close();
        }

        #endregion

        #region PrivateMethod

        private bool SaveFetionSetting(FetionSettingEntity fetionSetting)
        {
            FetionSettingEntity searchSetting = this.FindFetionSettingByUid(fetionSetting.Uid);

            //密码加密
            fetionSetting.FetionPass = SecurityHelper.EncryptDES(fetionSetting.FetionPass, Init.EncodeKey);

            if (searchSetting != null)
            {
                if (searchSetting.AutoLogin == fetionSetting.AutoLogin &&
                    searchSetting.FetionId == fetionSetting.FetionId &&
                    searchSetting.FetionPass == fetionSetting.FetionPass &&
                    searchSetting.Uid == fetionSetting.Uid)
                {
                    return false;
                }
                else
                {
                    this.fetionSettings.Remove(searchSetting);
                }
            }
            this.fetionSettings.Add(fetionSetting);
            return XmlHelper.XmlSerialize(Init.FetionSettingPath, this.fetionSettings, typeof(List<FetionSettingEntity>));
        }
        /// <summary>
        /// 获取界面设置
        /// </summary>
        /// <returns></returns>
        public FetionSettingEntity GetFetionSetting()
        {
            try
            {
                FetionSettingEntity setting = new FetionSettingEntity();

                if (this.txtFetionId.Text != "")
                {
                    setting.FetionId = this.txtFetionId.Text;
                }
                if (this.txtFetionPass.Text != "")
                {
                    setting.FetionPass = SecurityHelper.EncryptDES(this.txtFetionPass.Text, Init.EncodeKey);
                }

                if(this.txtBeforeMinutes.Text != "")
                {
                    setting.BeforeAlertMinutes = int.Parse(this.txtBeforeMinutes.Text);
                }

                setting.Uid = Init.Kaixin001Helper.User.Uid;
                setting.AutoLogin = true;
                return setting;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 获取存在的配置
        /// </summary>
        /// <returns></returns>
        public FetionSettingEntity GetFetionConfig()
        {

            if (File.Exists(Init.FetionSettingPath))
            {
                this.fetionSettings = (List<FetionSettingEntity>)XmlHelper.XmlDeserialize(Init.FetionSettingPath, typeof(List<FetionSettingEntity>));

                if (this.fetionSettings != null)
                {
                    for (int i = 0; i < fetionSettings.Count; i++)
                    {
                        FetionSettingEntity temp = fetionSettings[i];

                        if (temp.Uid == Init.Kaixin001Helper.User.Uid)
                        {
                            return temp;
                        }
                    }
                }
                else
                {
                    this.fetionSettings = new List<FetionSettingEntity>();
                }
            }
            return null;
        }

        /// <summary>
        /// 初始化信息
        /// </summary>
        private void FormInit()
        {
            FetionSettingEntity setting = this.GetFetionConfig();
            if (setting != null)
            {
                this.txtFetionId.Text = setting.FetionId;
                this.txtFetionPass.Text = SecurityHelper.DecryptDES(setting.FetionPass,Init.EncodeKey);
                this.txtBeforeMinutes.Text = setting.BeforeAlertMinutes.ToString();
            }
        }

        public FetionSettingEntity FindFetionSettingByUid(string account)
        {
            foreach (FetionSettingEntity temp in this.fetionSettings)
            {
                if (temp.Uid == account)
                {
                    return temp;
                }
            }
            return null;
        }

        #endregion
    }
}
